package com.webwork.mood.record.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.mood.common.dao.impl.SimpleDao;
import com.webwork.mood.record.dao.RuleDao;
import com.webwork.mood.record.dataobject.Rule;
import com.webwork.mood.util.ColumnLimit;
import com.webwork.mood.util.DBSimpleSQL;
import com.webwork.mood.util.StrManager;

@Repository
public class RuleDaoImpl extends SimpleDao implements RuleDao {

	public RuleDaoImpl() {
		super();
		this.dbTable = "t_rule";
		dbColumns.clear();
		dbColumns.put("name", "NAME");
		dbColumns.put("value", "VALUE");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof Rule))
			return new ColumnLimit();
		Rule o = (Rule) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getName())) {
			ret.add(dbColumns.get("name") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		Rule o = (Rule) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getName())) {
			ret.add(o.getName());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		Rule o = (Rule) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getName());
		objs.add(o.getValue());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		Rule ret = new Rule();
		ret.setName((String) map.get(dbColumns.get("name")));
		ret.setValue((String) map.get(dbColumns.get("value")));
		return ret;
	}
	
	@Override
	public void insert(Rule o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(Rule o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("name") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getName());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(Rule o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("name") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getName());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public List<Rule> query(Rule o, int page, int size) {
		List<Rule> ret = new ArrayList<Rule>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((Rule) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(Rule o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}

}
